/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.request.ShapeSize;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.shaper.CircleImageShaper;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Locale;

/**
 * CircleImageShaperTestPageView
 */
public class CircleImageShaperTestPageView extends DirectionalLayout implements Slider.ValueChangedListener {
    private SketchImageView mImageCircle;
    private Text mTvStroke;
    private Slider mSliderStroke;
    private CircleImageShaper mCircleImageShaper;
    private DisplayOptions mDisplayOptions;

    public CircleImageShaperTestPageView(Context context) {
        super(context, null);
        initView();
    }

    private void initView() {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        Component view = layoutScatter.parse(ResourceTable.Layout_circle_image_shaper_page_view, this, true);

        mImageCircle = (SketchImageView) view.findComponentById(ResourceTable.Id_image_circleImageShaper);

        mTvStroke = (Text) view.findComponentById(ResourceTable.Id_text_circleImageShaper_stroke);

        mSliderStroke = (Slider) view.findComponentById(ResourceTable.Id_slider);
        mSliderStroke.setMaxValue(100);
        mSliderStroke.setValueChangedListener(this);
        mSliderStroke.setProgressValue(5);

        final int componentWidth = Utils.getDisplayWidthInPx(getContext())
                - Utils.vp2px(getContext(), getContext().getString(ResourceTable.String_space_large)) * 2;
        mCircleImageShaper = new CircleImageShaper().setContext(getContext()).setResID(ResourceTable.Media_sample)
                .setComponentWidth(componentWidth);
        mDisplayOptions = new DisplayOptions().setShapeSize(new ShapeSize(componentWidth, componentWidth, Image.ScaleMode.CLIP_CENTER));

        mImageCircle.setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                int height = component.getEstimatedHeight();
                mCircleImageShaper.setImageHeight(height);
                apply();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    /**
     * apply
     */
    public void apply() {
        final float scaleRatio = 1.6f;
        mCircleImageShaper.setStroke(Color.getIntColor("#FFFFFF"), (int) (mSliderStroke.getProgress() * scaleRatio));
        mDisplayOptions.setShaper(mCircleImageShaper);
        mImageCircle.setOptions(mDisplayOptions);

        Sketch.with(getContext()).displayFromResource(ResourceTable.Media_sample, mImageCircle).commit();
    }

    @Override
    public void onProgressUpdated(Slider slider, int i1, boolean b1) {
        mTvStroke.setText(String.format("%d/%d", i1, 100, Locale.ENGLISH));
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {
        apply();
    }
}
